//
//  HEShareCirlceViewController.m
//  HealthFamily
//
//  Created by 吴欧 on 2021/3/2.
//  Copyright © 2021 honglutec. All rights reserved.
//

#import "HEShareCirlceViewController.h"
#import "HECircleChatManager.h"
#import "LEEAlert.h"
#import "JPSuspensionEntrance.h"

@interface HEShareCirlceViewController ()<UITableViewDelegate, UITableViewDataSource, HECircleChatManagerDelegate>

@property (nonatomic, strong) GDTableView *tableView;

@property (nonatomic, strong) NSMutableArray <HEChatRoomModel *>*dataSource;

@end

@implementation HEShareCirlceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
     
    self.title = @"分享到圈子";
    self.view.backgroundColor = CViewBgColor;
    
    [self.view addSubview:self.tableView];
    
    [self setCustomTableView:self.tableView];
    [self showEmpty];
    [self refrehData];
}

- (GDTableView *)tableView {
    if (!_tableView) {
        _tableView = [[GDTableView alloc] initWithFrame:CGRectMake(0, ZXNavBarHeight , KScreenWidth, KScreenHeight - ZXNavBarHeight) style:UITableViewStylePlain];
        _tableView.separatorColor = CSplitLineColor;
        _tableView.scrollsToTop = YES;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0.01)];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0.01)];

        _tableView.backgroundColor = CViewBgColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (void)refrehData {
    self.dataSource = [NSMutableArray arrayWithArray: [HECircleChatManager sharedManager].teams];
    if (self.dataSource.count == 0) {
        [[HECircleChatManager sharedManager] addDelegate:self];
        [[HECircleChatManager sharedManager] initOpration];
    }
    [self.tableView reloadData];
}

#pragma mark - HECircleChatManagerDelegate
- (void)allTeams:(NSArray <HEChatRoomModel *> *)teams {
    self.dataSource = [NSMutableArray arrayWithArray:teams];
    [self.tableView reloadData];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
        [cell.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.offset(40);
            make.centerY.equalTo(cell.contentView);
            make.left.equalTo(cell.contentView).offset(15);
        }];
        
        cell.imageView.layer.masksToBounds = YES;
        cell.imageView.layer.cornerRadius = 20;
        
        [cell.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(cell.contentView);
            make.left.equalTo(cell.imageView.mas_right).offset(20);
            make.right.equalTo(cell.contentView).offset(-15);
        }];
    }
    
    HEChatRoomModel *info = _dataSource[indexPath.row];
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:info.roomIcon] placeholderImage: IMAGE_NAMED(@"noImage")];
    cell.textLabel.text = info.roomName;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    HEChatRoomModel *room = _dataSource[indexPath.row];
    [self popLogoutAlter: room];
}


- (void)popLogoutAlter:(HEChatRoomModel *)room {
    KWeakSelf
    UIColor *blueColor = [UIColor colorWithRed:90/255.0f green:154/255.0f blue:239/255.0f alpha:1.0f];
    [LEEAlert alert].config
    .LeeAddContent(^(UILabel *label) {
        label.text = [NSString stringWithFormat:@"您确定要分享到%@吗？", room.roomName];
        label.font = DYBlodFont(16);
        label.textColor = [[UIColor whiteColor] colorWithAlphaComponent:0.75f];
    })
    .LeeAddAction(^(LEEAction *action) {
        action.type = LEEActionTypeCancel;
        action.title = @"取消";
        action.titleColor = blueColor;
        action.backgroundColor = [UIColor whiteColor];
        action.clickBlock = ^{
            // 取消点击事件Block
        };
    })
    .LeeAddAction(^(LEEAction *action) {
        action.type = LEEActionTypeDefault;
        action.title = @"确定";
        action.titleColor = blueColor;
        action.backgroundColor = [UIColor whiteColor];
        action.clickBlock = ^{
             
            // 确定点击事件Block
            [[HECircleChatManager sharedManager] switchRoomVC:room isFromChatPage:weakSelf.model.isFromChatPage callback:^(HEIMCircleChatController * _Nonnull vc) {
                [vc sendMessageWithVC: [weakSelf shareInfoMessage: room]];
            }];
             
//            JPSEInstance.suspensionView = nil;
//            [[HECircleChatManager sharedManager] leaveRoom];
//            [self.navigationController popViewControllerAnimated:YES];
        };
    })
    .LeeHeaderColor(blueColor)
    #ifdef __IPHONE_13_0
    .LeeUserInterfaceStyle(UIUserInterfaceStyleLight)
    #endif
    .LeeShow();
}

- (HEIMMessageModel *)shareInfoMessage:(HEChatRoomModel *)room {
    
    HEIMInfoPageObject *infoPage = [HEIMInfoPageObject new];
    infoPage.id = _model.id;
    infoPage.voiceUrl = _model.audio;
    infoPage.iconUrlStr = _model.img_url;
    infoPage.iconURL = [NSURL URLWithString:_model.img_url];
    infoPage.title = _model.info_title;
    infoPage.infoUrl = _model.infoUrl;
    infoPage.subTitle = _model.information_brief;

    HEIMMessage *message = [[HEIMMessage alloc] init];
    message.type = _messageType;
    message.from = [UserManager sharedUserManager].curUserInfo.id;
    message.actor = HEActorTypeUser;
    message.to = [NSString stringWithFormat:@"%ld", room.roomId];
    message.messageObject = infoPage;
    message. modelType = HEIMUserModelTypeRoom;

    HEIMMessageModel *model = [[HEIMMessageModel alloc] initWithMessage:message];
    model.deliveryState = HEIMMessageDeliveryStateDelivering;
    return model;
}

@end
 
